package cn.hc.sp02.item.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import sp01.pojo.Item;
import sp01.service.ItemService;
import web.util.JsonResult;

import java.util.List;
import java.util.Random;

/**
 * @author 海绵宝宝
 * date 2021/10/26 16:52
 */
@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Value("${server.port}")
    private int port;
    //获取商品列表
    @GetMapping("/{orderIds}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderIds) throws InterruptedException {
        log.info("获取订单的商品列表，orderId="+orderIds);
        if(Math.random()<0.9){
            int t = new Random().nextInt(5000);
            log.info("延迟："+t);
            Thread.sleep(t);
        }

        List<Item> items = itemService.getItems(orderIds);
        JsonResult j = JsonResult.ok().msg("port="+port).data(items);
        System.out.println(port);
        return j;
    }
    //减少商品库存
    @PostMapping("/delete")
    public JsonResult<?> delete(@RequestBody List<Item> list){
        itemService.decreaseNumbers(list);
        return JsonResult.ok().msg("减少商品库存成功");
    }

}
